package org.wzl.todolisthelper.config.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.wzl.todolisthelper.utils.IdCommonGen;

@Component
public class TraceIdInterceptor implements HandlerInterceptor {
 
    /**
     * preHandle为请求前拦截
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
 
        // 在日志框架中的MDC中添加请求的唯一标识（用于返回接口及保存mysql）
        String traceId = String.valueOf(IdCommonGen.generateId());
 
        // 绑定key值到线程中
        MDC.put("traceId", traceId);
        // 继续执行接口请求
        return true;
    }
}